package com.ruoyi.project.cms.content.service;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.content.domain.Content;
import com.ruoyi.project.cms.content.domain.ContentVo;
import com.ruoyi.project.cms.content.mapper.ContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author 黄药师
 * @date 2021-01-30 15:40
 * @desc 巨梯学院 http://www.jutixueyuan.com
 */
@Service
public class CmsContentServicelmpl implements ICmsContentService{

    @Autowired
    private ContentMapper contentMapper;
    /**
     *  查询 content
     * @param content
     * @return
     */
    @Override
    public List<ContentVo> selectContentList(Content content) {
        return contentMapper.selectContentList(content);
    }

    /**
     *  校验 contentTilte是否唯一
     * @param content
     * @return
     */
    @Override
    public String selectContentTitleUnique(Content content) {
        String contentTitle = content.getContentTitle();

        //查询
        Content contentInfo = contentMapper.selectContentTitleUnique(contentTitle);
        if (Objects.isNull(contentInfo)){
            return "0";
        }
        return "1";
    }

    @Override
    public int insertContent(Content content) {
        content.setCreateBy(content.getCreateBy());
       return contentMapper.insertContent(content);

    }

    /**
     * 通过 id查询 content
     * @param contentId
     * @return
     */
    @Override
    public Content selectContentById(Integer contentId) {

        return contentMapper.selectContentById(contentId);

    }

    @Override
    public int updateContent(Content content) {
        // 谁修改了
        content.setUpdateBy(ShiroUtils.getLoginName());
        return contentMapper.updateContent(content);
    }

    @Override
    public int deleteConTentByIds(String ids) {

        // ids 转成一个一个数组
        Long[] contentIds = Convert.toLongArray(ids);

        return contentMapper.deleteConTentByIds(contentIds);
    }

    @Override
    public int selectContentCountByTypeId(Long typeId) {
        return contentMapper.selectContentCountByTypeId(typeId);
    }
}
